<template>
  <el-dialog :title="dialogProps.title" :visible.sync="dialogProps.visible" width='550px'  :fullscreen = 'Global.currentDevice'>
        <el-tabs  >
              <el-tab-pane label="部门信息" name="first"></el-tab-pane>
        </el-tabs>
        <el-row>
              <el-col :xs='24' :lg='20'>
                    <el-form  label-width="120px" align='left' :model="form" ref='form' :rules='rules'  :label-position='Global.dialog_label_Position'>
                            <el-form-item label="名称" prop='name'>
                                  <el-input v-model.trim="form.name"     placeholder='请输入部门名称'></el-input>
                            </el-form-item>
                            <el-form-item label="电话" prop='phone'>
                                  <el-input v-model.trim="form.phone"      placeholder='请输入联系电话'></el-input>
                            </el-form-item>
                            <el-form-item label="地址"><el-input v-model.trim="form.address"  placeholder='请输入地址'></el-input></el-form-item>
                            <el-form-item label="上级部门">{{currentApartNode}}
                                    <el-button  class='export-icon ml10'  size="small" round  @click="change()">变更</el-button>
                            </el-form-item>
                            <el-form-item label="部门角色" prop='roleCodes'>
                                <el-select v-model.trim="roleCodes" multiple placeholder="搜索选择角色" filterable class='w-100' >
                                      <el-option
                                        v-for="item in roleList"
                                        :key="item.code"
                                        :label="item.name"
                                        :value="item.code">
                                      </el-option>
                                </el-select>
                            </el-form-item>
                    </el-form>
              </el-col>
        </el-row>
        <div slot="footer" class="dialog-footer">
              <el-button @click="dialogProps.visible = false">取 消</el-button>
              <el-button type="primary" @click="submit('form')">确 定</el-button>
        </div>
        <chooseApart ref="choose_apart" :parentlist='parentgroup' v-on:save="changeResult(arguments)"  :selfCode='selfCode'></chooseApart>
  </el-dialog>
</template>

<script charset="utf-8" type="text/javascript">
/*接口*/
import { saveDepart,getParentName,modifyDepart}  from  '@/api/user/index'
 
//*****组件***** 
//                新增/编辑
import  chooseApart  from '@/components/userlist/chooseApart'
export default {
  name: 'EditUserGroup',
  props:{
    parentgroup: {
                    type: Array,
                    default:function(){
                          return [];
                    }
                },
    roleList : {
                    type: Array,
                    default:function(){
                          return [];
                    }
                },
  },
  data() {
    return {
      loading: false,
      //dialogo基本信息  显示隐藏状态  title 
      dialogProps: {
        visible: false,
        action: '',
        title: '新增部门'
      },

      options: [],
        value5: [],
        value11: [],

        rules:{
          name:[
            {required:true,message:'请输入部门名称'}
          ],
          tel:[
            { validator: this.checkTel ,trigger: 'blur'}
          ]

        },


        currentApartNode:'无',
      //绑定form
      form:{
          code:'',
          name:'',
          phone:'',
          address:'',

          parentCode:'',
          roles:[],
      },
      roleCodes:[],
      selfCode:'',//自己的code，要控制更改部门时不让他选择为自己或则下级


      isEdit:false,

    }
  },
  created(){

    
  },
  methods: {
    //变更上级部门
    change(){
        this.$refs.choose_apart.$emit('openChoose',this.form.parentCode)
    },
    //变更上级部门结果
    changeResult(arg){
      
      [this.form.parentCode,this.currentApartNode]=[arg[0],arg[1]];

      
    },
    submit(formName) {
      let that = this
      this.loading = true
      //先验证表达
      this.$refs[formName].validate(valid => {
          //通过验证后

          if (valid) {
                  this.form.roles = []//先置为空

                  this.roleCodes.forEach((item)=>{

                            this.form.roles.push({code:item})
                        })
                  // console.log(this.roleCodes)
                  // console.log(this.form.roles)
                  if(this.isEdit == false){
                        
                        saveDepart(this.form).then(res=>{
                          if(res.data.flag == true){
                              this.$message({type:'success',message:res.data.msg})
                              this.$emit('save-finished')
                              this.dialogProps.visible=false 
                          }
                          
                        })
                  }
                  else{
                        modifyDepart(this.form).then(res=>{
                          if(res.data.flag == true){
                              this.$message({type:'success',message:res.data.msg})
                              this.$emit('save-finished')
                              this.dialogProps.visible=false 
                          }
                          
                        })
                  }
                  
          } 
          else {
            return false;
          }
      });

      
    },
    
  },
  mounted: function() {
    this.$nextTick(() => {
      this.$on('opengroup', function(name,code) {
        //基本设置
        this.isEdit=false
        this.dialogProps.title = '新增部门'
        this.dialogProps.visible = true

        //重置
        this.resetForm('form')
        this.form={
          code:'',
          name:'',
          phone:'',
          address:'',
          parentCode:'',
          roles:[],
        };
        this.roleCodes=[]

        //信息赋值
        this.currentApartNode=name
        this.form.parentCode=code

      })
      this.$on('openEditgroup', function(data,code) {
        //基本设置
        this.isEdit=true
        this.dialogProps.title = '编辑部门'
        this.dialogProps.visible = true
        this.selfCode = code

        //重置
        this.roleCodes=[]

        //信息赋值
        let proxy = new Proxy(data, {
          get: function(target, property) {
            if(property == 'address' || 
               property == 'code' || 
               property == 'name' || 
               property == 'parentName'  || 
               property == 'phone' || 
                property == 'roles' || 
               property ==  'parentCode')

            return target[property];
          }
        });
        this.form=proxy;
        this.currentApartNode=this.form.parentName
        if(this.form.roles!=null){
            this.form.roles.forEach((item)=>{
                this.roleCodes.push(item.code)
            })
        }
        
      })

    })
  },
  updated(){
    
  },
  components:{
    chooseApart
  }
}
</script>

<style  scoped>
</style>


